<template>
    <van-nav-bar title="地址" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-address-list v-model="chooseAddressId" :list="list" :disabled-list="disabledList" disabled-text="以下地址超出配送范围"
        default-tag-text="默认" @add="onAdd" @edit="onEdit" v-show="!choose" />
    <van-action-sheet v-model:show="choose">
        <van-address-edit :area-list="areaList" :address-info="addressInfo" show-delete show-set-default
            show-search-result :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']"
            @save="onSave" @delete="onDelete" @change-default="defaultFlag" v-show="choose" />
    </van-action-sheet>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { areaList } from '@vant/area-data';
import { useCounterStore } from "@/stores/counter"
import { addAddress, mrAddress, addressList, deleAddress, updateAddress } from "@/service/order"
import { ref } from "vue"
let router = useRouter()
let store = useCounterStore()
let choose = ref(false)
let arealist = ref([] as any)
let userid = ref((store as any).userInfo.userid)
let type = ref(0)
let addressInfo = ref<any>("")
let list = ref([] as any)
let chooseAddressId = ref("")
let defaul = ref(null)
let isDefault = ref(null)
let disabledList = ref([ {
          id: '5',
          name: '666',
          tel: '13254278278',
          address: '贵州贵阳',
        }] as any)
let searchResult = ref([] as any)
const onClickLeft = () => {
    router.push("/user")
}
const onAdd = () => {
    choose.value = true
    arealist.value = { areaList }.areaList
    type.value = 1
    addressInfo.value = {}
}
const onEdit = (dataEdit: any) => {
    type.value = 2
    addressInfo.value = dataEdit
    arealist.value = { areaList }.areaList
    choose.value = true
}
const getAddress = () => {
    addressList(userid.value).then((res: any) => {
        if (res.code === "200") {
            list.value = res.data.map((item: any) => ({
                id: item.addressid,
                tel: item.tel,
                name: item.name,
                address: item.province + item.city + item.county + item.addressDetail,
                province: item.province,
                city: item.city,
                county: item.county,
                addressDetail: item.addressDetail,
                isDefault: item.isDefault
            }))
        }
    })
}
const defaultAddress = ()=>{
    mrAddress(userid.value).then((res:any)=>{
        if(res.data.length == 0){
            chooseAddressId.value=""
        }else{
            chooseAddressId.value=res.data.at(0).addressid
            defaul.value =  res.data[0].isDefault
        }
    })
}
const defaultFlag = (flag:any)=>{
    isDefault.value = flag
}
const onDelete = (addressid:any)=>{
    deleAddress(addressid.id).then((res:any)=>{
        if(res.code === "200"){
            choose.value = false
            getAddress()
        }
    })
}
const onSave = (data:any)=>{
    if(type.value === 1){
        let temp = ref({
            userid: userid.value,
          name: data.name,
          tel: data.tel,
          province: data.province,
          city: !data.city ? '' : data.city,
          county: !data.datacounty ? '' : data.datacounty,
          addressDetail: data.addressDetail,
          isDefault: data.isDefault
        } as any)
        addAddress(temp.value).then((res:any)=>{
            if(res.code ==="200"){
                choose.value = false
                defaultAddress()
                getAddress()
            }
            type.value = 0
        })
    }
    else if(type.value === 2){
        if(data.isDefault == defaul.value){
            let temp = ref({ addressid: chooseAddressId.value,
            userid: userid.value,
            name: data.name,
            tel: data.tel,
            province: data.province,
            city: !data.city ? '' : data.city,
            county: !data.datacounty ? '' : data.datacounty,
            addressDetail: data.addressDetail,
            isDefault: false} as any)
            updateAddress(temp.value).then((res:any)=>{
                if(res.code === "200"){
                    console.log(res)
                }
            })
        }
        let temp2 = ref({
            addressid: data.id,
          userid: userid.value,
          name: data.name,
          tel: data.tel,
          province: data.province,
          city: !data.city ? '' : data.city,
          county: !data.datacounty ? '' : data.datacounty,
          addressDetail: data.addressDetail,
          isDefault: isDefault.value
        } as any) 
        updateAddress(temp2.value).then((res:any)=>{
            if(res.code === "200"){
                choose.value = false
                defaultAddress()
                getAddress()
            }
            type.value = 0
            isDefault.value = null
        })
    }
}
getAddress()
defaultAddress()
</script>

<style scoped>

</style>